/*
 * Encapsulates all the server-side client state. 
 * Contains a reference to the current opponent 
 * state as well as the current game. Has a lock for 
 * synchronization between player, client threads.
 */

package egs.server;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import egs.comms.Connection;
import egs.games.Game;

public class ClientState {
	public final static int CONTROL = 1;
	public final static int UNAUTHENTICATED = 2;
	public final static int AUTHENTICATED = 3;
	public final static int GAME_SELECTED = 4;
	public final static int OPPONENT_WAIT = 5;
	public final static int IN_GAME = 6;
	public final static int MAKE_MOVE = 7;
	
	private Connection playerConnection;
	private String username;
	private ClientState oppState;
	private GameManager.GameKey selectedGameKey;
	private Game selectedGame;
	private int playerNumber;
	private int state;
	private Lock lock;
	
	public ClientState(Connection serverConnection) {
		playerConnection = serverConnection;
		username = null;
		oppState = null;
		selectedGame = null;
		playerNumber = 0;
		state = CONTROL;
		lock = new ReentrantLock();
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	public ClientState getOppState() {
		return oppState;
	}

	public void setOppState(ClientState oppState) {
		this.oppState = oppState;
	}

	public GameManager.GameKey getSelectedGameKey() {
		return selectedGameKey;
	}

	public void setSelectedGameKey(GameManager.GameKey selectedGameKey) {
		this.selectedGameKey = selectedGameKey;
	}
	
	public Game getSelectedGame() {
		return selectedGame;
	}

	public void setSelectedGame(Game selectedGame) {
		this.selectedGame = selectedGame;
	}
	
	public int getPlayerNumber() {
		return playerNumber;
	}

	public void setPlayerNumber(int playerNumber) {
		this.playerNumber = playerNumber;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public Connection getPlayerConnection() {
		return playerConnection;
	}
	
	public void setPlayerConnection(Connection connection){
		this.playerConnection = connection;
	}
	public void lock() {
		lock.lock();
	}
	
	public void unlock() {
		lock.unlock();
	}
	
	
}
